#!/bin/bash


if [ -z "$1" ] || [ -z "$2" ]
then
    echo "Usage: `basename $0` NewDir OldDir"
    exit 1
fi

if [ ! -d "$1" ] || [ ! -d "$2" ]
then
    echo $1 is not a directory, but should be
    exit 1
fi

# create file header
NEWVER=`echo $1 | awk '{ gsub( /^[a-zA-Z]+_/, "", $0 ); gsub( "_", ".", $0 ); gsub( "/", "", $0 ); print $0 }'`
OLDVER=`echo $2 | awk '{ gsub( /^[a-zA-Z]+_/, "", $0 ); gsub( "_", ".", $0 ); gsub( "/", "", $0 ); print $0 }'`
OUTPUT="history-$NEWVER.txt"

echo "This release is for HP INTERNAL USE ONLY"				>  "$OUTPUT"
echo "------------------------------------------------------------"	>> "$OUTPUT"
echo ""									>> "$OUTPUT"
echo "DOCUMENT INFORMATION"						>> "$OUTPUT"
echo ""									>> "$OUTPUT"
echo "Filename: history.txt"						>> "$OUTPUT"
echo "Owner: CHP"							>> "$OUTPUT"
echo "Product identifier: EBSU $NEWVER"					>> "$OUTPUT"
echo "Project identifier: EBSU $NEWVER"					>> "$OUTPUT"
echo "Current lifecycle phase: QA-Build-$NEWVER"			>> "$OUTPUT"
echo ""									>> "$OUTPUT"
echo "EBSU for EFI"							>> "$OUTPUT"
echo "======"								>> "$OUTPUT"
echo ""									>> "$OUTPUT"
echo "$NEWVER"								>> "$OUTPUT"
echo "======"								>> "$OUTPUT"
echo ""									>> "$OUTPUT"
echo "    Date: "`date +"%b %d, %Y"`					>> "$OUTPUT"
echo "    Author: Lance Zhao"						>> "$OUTPUT"
echo "    Previous Verion: $OLDVER"					>> "$OUTPUT"
echo "    "								>> "$OUTPUT"
echo "    Fixed Bugs:"							>> "$OUTPUT"
echo "        - None."							>> "$OUTPUT"
echo "        "								>> "$OUTPUT"
echo "    Changes:"							>> "$OUTPUT"
echo "        - None."							>> "$OUTPUT"
echo ""									>> "$OUTPUT"
echo "    Files changed on this release:"				>> "$OUTPUT"
echo "        M: modified"						>> "$OUTPUT"
echo "        C: recompiled"						>> "$OUTPUT"
echo "        R: removed"						>> "$OUTPUT"
echo "        N: new"							>> "$OUTPUT"
echo ""									>> "$OUTPUT"

# create temporary files 

NEWFILE=".abc1"
OLDFILE=".abc2"
trap "rm -f $NEWFILE $OLDFILE; exit 1" INT


# merge in file changes 

function output()
{
    echo "        $1   $2" | awk -F '/' '{ printf("%s", $1); for (i = 2; i <= NF; i++ ) { printf("\\%s", $i) } printf("\n") }'	>> "$OUTPUT"
}

(cd $1 ; find * -type f) 1> $NEWFILE 2>/dev/null
(cd $2 ; find * -type f) 1> $OLDFILE 2>/dev/null

for filename in `cat $NEWFILE $OLDFILE | grep -v .svn | sort -u`
do 

    if [ -e $1/$filename ] && [ -e $2/$filename ]
    then
        cmp -s $1/$filename $2/$filename
        if [ $? -ne 0 ]
        then
            # file is different, copy over new revision
            MC=`echo $filename | awk '{ if (  $0 ~ /BootIA64\.efi$/||/configutils.*\.efi$/ ) { print "C" } else { print "M" } }'`
            output $MC "$filename"
        else
            output " " "$filename"
        fi
        
    elif [ -e $2/$filename ]
    then 
        # this filename was removed in the new revision
        output "R" "$filename"

    elif [ -e $1/$filename ]
    then 
        # this filename is a new filename
        output "N" "$filename"
    fi

done


# remove temporary files 

rm -f $NEWFILE $OLDFILE
unix2dos $OUTPUT

